package cn.iocoder.an.module.mold.controller.admin.keeptask;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.framework.excel.core.util.ExcelUtils;
import cn.iocoder.an.module.mold.controller.admin.equipmnetkeepwait.vo.EquipmnetKeepWaitPageReqVO;
import cn.iocoder.an.module.mold.controller.admin.equipmnetkeepwait.vo.EquipmnetKeepWaitRespVO;
import cn.iocoder.an.module.mold.controller.admin.keeptask.vo.KeepTaskPageReqVO;
import cn.iocoder.an.module.mold.controller.admin.keeptask.vo.KeepTaskRespVO;
import cn.iocoder.an.module.mold.controller.admin.keeptask.vo.KeepTaskSaveReqVO;
import cn.iocoder.an.module.mold.dal.dataobject.equipmnetkeepwait.EquipmnetKeepWaitDO;
import cn.iocoder.an.module.mold.dal.dataobject.keeptask.KeepTaskDO;
import cn.iocoder.an.module.mold.service.equipmnetkeepwait.EquipmnetKeepWaitService;
import cn.iocoder.an.module.mold.service.keeptask.KeepTaskService;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 保养任务")
@RestController
@RequestMapping("/Damage/keep-task")
@Validated
@Controller("KeepTaskControllerDamage")
public class KeepTaskController {

    @Autowired
    private KeepTaskService keepTaskService;


    @Autowired
    private EquipmnetKeepWaitService equipmnetKeepWaitService;

    @PostMapping("/create")
    @Operation(summary = "创建保养任务")
    public CommonResult<Long> createKeepTask(@Valid @RequestBody KeepTaskSaveReqVO createReqVO) {
        return success(keepTaskService.createKeepTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养任务")
    public CommonResult<Boolean> updateKeepTask(@Valid @RequestBody KeepTaskSaveReqVO updateReqVO) {
        keepTaskService.updateKeepTask(updateReqVO);
        return success(true);
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除保养任务")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteKeepTask(@RequestParam("id") Long id) {
        keepTaskService.deleteKeepTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除保养任务")
    public CommonResult<Boolean> deleteKeepTaskList(@RequestParam("ids") List<Long> ids) {
        keepTaskService.deleteKeepTaskListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得保养任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<KeepTaskRespVO> getKeepTask(@RequestParam("id") Long id) {
        KeepTaskDO keepTask = keepTaskService.getKeepTask(id);
        Long waitId = keepTask.getWaitId();
        EquipmnetKeepWaitDO equipmentKeepWait = equipmnetKeepWaitService.getEquipmnetKeepWait(waitId);
        EquipmnetKeepWaitRespVO waitBean = BeanUtils.toBean(equipmentKeepWait, EquipmnetKeepWaitRespVO.class);
        KeepTaskRespVO taskBean = BeanUtils.toBean(keepTask, KeepTaskRespVO.class);
        taskBean.setWaitRespVO(waitBean);
        return success(taskBean);
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得保养任务分页")
    public CommonResult<PageResult<KeepTaskRespVO>> getKeepTaskPage(@Valid KeepTaskPageReqVO pageReqVO) {
        PageResult<KeepTaskDO> pageResult = keepTaskService.getKeepTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeepTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养任务 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKeepTaskExcel(@Valid KeepTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeepTaskDO> list = keepTaskService.getKeepTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养任务.xls", "数据", KeepTaskRespVO.class,
                        BeanUtils.toBean(list, KeepTaskRespVO.class));
    }

    @TransMethodResult
    @GetMapping("/statistics")
    @Operation(summary = "获得保养统计")
    public CommonResult<PageResult<EquipmnetKeepWaitRespVO>> getRepairStatistics(EquipmnetKeepWaitPageReqVO pageReqVO) {
        return success(keepTaskService.getKeepStatistics(pageReqVO));
    }

}